//Title: Hovercard plugin by PC //Documentation: http://designwithpc.com/Plugins/Hovercard (function ($) { $.fn.hovercard = function (options) { var defaults = { width: 300, openOnLeft: false, openOnTop: false, cardImgSrc: "", detailsHTML: "", twitterScreenName: '', showTwitterCard: false, facebookUserName: '', showFacebookCard: false, showCustomCard: false, customCardJSON: {}, customDataUrl: '', background: "#ffffff", delay: 0, autoAdjust: true, onHoverIn: function () { }, onHoverOut: function () { } }; var options = $.extend(defaults, options); if ($('#css-hovercard').length <= 0) { var hovercardTempCSS = '")'; $(hovercardTempCSS).appendTo('head'); } //Executing functionality on all selected elements return this.each(function () { var obj = $(this); var hassanHTML, hassanIMG=''; //wrap a parent span to the selected element obj.wrap('
'); //add a relatively positioned class to the selected element obj.addClass("hc-name"); //Added by Hassan hassanHTML =options.detailsHTML; if (options.detailsHTML=='') hassanHTML=$(this).closest('.hc-name').attr('data-hover-content'); hassanIMG =options.cardImgSrc; if (options.cardImgSrc=='') hassanIMG=$(this).closest('.hc-name').attr('data-hover-image'); var hcImg=''; if (typeof hassanIMG !='undefined' && hassanIMG.length > 0) { hcImg = ''; } //Hassan deleted: hcImg+ options.cardImgSrc //generate details span with html provided by the user var hcDetails = '
' + hcImg + hassanHTML + '
'; //append this detail after the selected element obj.after(hcDetails); obj.siblings(".hc-details").eq(0).css({ 'width': options.width, 'background': options.background }); //toggle hover card details on hover obj.closest(".hc-preview").hover(function () { var $this = $(this); adjustToViewPort($this); //Up the z indiex for the .hc-name to overlay on .hc-details $this.css("zIndex", "200"); obj.css("zIndex", "100").find('.hc-details').css("zIndex", "50"); if (options.noOverride) obj.css("zIndex", "0").find('.hc-details').css("zIndex", "100"); var curHCDetails = $this.find(".hc-details").eq(0); curHCDetails.stop(true, true).delay(options.delay).fadeIn(); if (typeof options.onHoverIn == 'function') { if (options.showCustomCard && curHCDetails.find('.s-card').length <= 0) { var dataUrl = options.customDataUrl; if (typeof obj.attr('data-hovercard') == 'undefined') { //do nothing. detecting typeof obj.attr('data-hovercard') != 'undefined' didn't work as expected. } else if (obj.attr('data-hovercard').length > 0) { dataUrl = obj.attr('data-hovercard'); } LoadSocialProfile("custom", dataUrl, curHCDetails, options.customCardJSON); } if (options.showTwitterCard && curHCDetails.find('.s-card').length <= 0) { var tUsername = options.twitterScreenName.length > 0 ? options.twitterScreenName : obj.text(); if (typeof obj.attr('data-hovercard') == 'undefined') { } else if (obj.attr('data-hovercard').length > 0) { tUsername = obj.attr('data-hovercard'); } LoadSocialProfile("twitter", tUsername, curHCDetails); } if (options.showFacebookCard && curHCDetails.find('.s-card').length <= 0) { var fbUsername = options.facebookUserName.length > 0 ? options.facebookUserName : obj.text(); if (typeof obj.attr('data-hovercard') == 'undefined') { } else if (obj.attr('data-hovercard').length > 0) { fbUsername = obj.attr('data-hovercard'); } LoadSocialProfile("facebook", fbUsername, curHCDetails); } //Callback function options.onHoverIn.call(this); } }, function () { //Undo the z indices $this = $(this); $this.find(".hc-details").eq(0).stop(true, true).fadeOut(300, function () { $this.css("zIndex", "0"); obj.css("zIndex", "0").find('.hc-details').css("zIndex", "0"); if (typeof options.onHoverOut == 'function') { options.onHoverOut.call(this); } }); }); function adjustToViewPort(hcPreview) { var hcDetails = hcPreview.find('.hc-details').eq(0); var hcPreviewRect = hcPreview[0].getBoundingClientRect(); var hcdTop = hcPreviewRect.top - 20; //Subtracting 20px of padding; var hcdRight = hcPreviewRect.left + 35 + hcDetails.width(); //Adding 35px of padding; var hcdBottom = hcPreviewRect.top + 35 + hcDetails.height(); //Adding 35px of padding; var hcdLeft = hcPreviewRect.top - 10; //Subtracting 10px of padding; if (options.openOnLeft || (options.autoAdjust && (hcdRight > window.innerWidth))) { hcDetails.addClass("hc-details-open-left"); } else { hcDetails.removeClass("hc-details-open-left"); } if (options.openOnTop || (options.autoAdjust && (hcdBottom > window.innerHeight))) { hcDetails.addClass("hc-details-open-top"); } else { hcDetails.removeClass("hc-details-open-top"); } } function LoadSocialProfile(type, username, curHCDetails, customCardJSON) { var cardHTML, urlToRequest, customCallback, loadingHTML, errorHTML; switch (type) { case "twitter": { urlToRequest = 'http://api.twitter.com/1/users/lookup.json?screen_name=' + username; cardHTML = function (profileData) { profileData = profileData[0]; return '
' + (profileData.profile_image_url ? ('') : '') + (profileData.name ? ('') : '') + (profileData.screen_name ? ('(@' + profileData.screen_name + ')
') : '') + (profileData.location ? ('') : '') + (profileData.description ? ('

' + profileData.description + '

') : '') + (profileData.url ? ('' + profileData.url + '
') : '') + '' + '
'; }; loadingHTML = 'Contacting Twitter...'; errorHTML = 'Invalid username or you have exceeded Twitter request limit.
Please note, Twitter only allows 150 requests per hour.'; customCallback = function () { }; if ($('#t-follow-script').length <= 0) { var script = document.createElement('script'); script.type = 'text/javascript'; script.src = '//platform.twitter.com/widgets.js'; script.id = 't-follow-script'; $('body').append(script); } curHCDetails.append(''); } break; case "facebook": { urlToRequest = 'https://graph.facebook.com/' + username, cardHTML = function (profileData) { return '
' + '' + '
' + (profileData.link ? ('' + profileData.link + '
') : '') + (profileData.likes ? ('
') : '') + (profileData.description ? ('

' + profileData.description + '

') : '') + (profileData.start_time ? ('

Start Time:
' + profileData.start_time + '

') : '') + (profileData.end_time ? ('

End Time:
' + profileData.end_time + '

') : '') + (profileData.founded ? ('

Founded:
' + profileData.founded + '

') : '') + (profileData.mission ? ('

Mission:
' + profileData.mission + '

') : '') + (profileData.company_overview ? ('

Overview:
' + profileData.company_overview + '

') : '') + (profileData.products ? ('

Products:
' + profileData.products + '

') : '') + (profileData.website ? ('

Web:
' + profileData.website + '

') : '') + (profileData.email ? ('

Email:
' + profileData.email + '

') : '') + '
'; }; loadingHTML = "Contacting Facebook..."; errorHTML = "The requested user, page, or event could not be found. Please try a different one."; customCallback = function (profileData) { //Append the twitter script to the document to add a follow button if ($('#fb-like-script').length <= 0) { var script = document.createElement('script'); script.type = 'text/javascript'; script.text = '(function(d, s, id) {' + 'var js, fjs = d.getElementsByTagName(s)[0];' + 'if (d.getElementById(id)) {return;}' + 'js = d.createElement(s); js.id = id;' + 'js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=140270912730552";' + 'fjs.parentNode.insertBefore(js, fjs);' + '}(document, "script", "facebook-jssdk"));'; script.id = 'fb-like-script'; $('body').prepend(script); $('body').prepend('
'); } curHCDetails.append('
'); } } break; case "custom": { urlToRequest = username, cardHTML = function (profileData) { return '
' + (profileData.image ? ('') : '') + (profileData.name ? ('
') : '') + (profileData.link ? ('' + profileData.link + '
') : '') + (profileData.bio ? ('

' + profileData.bio + '

') : '') + (profileData.website ? ('

Web:
' + profileData.website + '

') : '') + (profileData.email ? ('

Email:
' + profileData.email + '

') : '') + '
'; }; loadingHTML = "Loading..."; errorHTML = "Sorry, no data found."; customCallback = function () { }; } break; default: { } break; } if ($.isEmptyObject(customCardJSON)) { $.ajax({ url: urlToRequest, type: 'GET', dataType: 'jsonp', //jsonp for cross domain request timeout: 4000, //timeout if cross domain request didn't respond, or failed silently beforeSend: function () { curHCDetails.find('.s-message').remove(); curHCDetails.append('

' + loadingHTML + '

'); }, success: function (data) { if (data.length <= 0) { curHCDetails.find('.s-message').html(errorHTML); } else { curHCDetails.find('.s-message').remove(); curHCDetails.prepend(cardHTML(data)); adjustToViewPort(curHCDetails.closest('.hc-preview')); curHCDetails.stop(true, true).delay(options.delay).fadeIn(); customCallback(data); } }, error: function (jqXHR, textStatus, errorThrown) { curHCDetails.find('.s-message').html(errorHTML); } }); } else { curHCDetails.prepend(cardHTML(customCardJSON)); } }; }); }; })(jQuery);